// MuSiCo.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace libmusico;
using namespace MuSiCo;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false);

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}

void Form1::OnTestButtonClick(Object^ o,EventArgs^ e)
{
	// Disabling the button
	button2->Enabled = false;

	// Parsing inputs
	Int32 channel = Convert::ToInt32(numericUpDown1->Value);
	Int32 note = Convert::ToInt32(numericUpDown2->Value);
	Int32 volume = Convert::ToInt32(numericUpDown3->Value);
	Int32 status = 144;
	if (comboBox1->Text == "ON"){status = 144;}
	if (comboBox1->Text == "OFF"){status = 128;}
	if (comboBox1->Text == "PC"){status = 192;}
	if (comboBox1->Text == "CMC"){status = 176;}

	// Creating message then send it
	test = gcnew libmusico::Message(channel, status, note, volume);
	libmusico::Command^ test_cmd = gcnew libmusico::Command();
	test_cmd->addMessage("msg", Form1::test);
	test_cmd->send(Form1::device);

	// Enabling the button
	button2->Enabled = true;
}

void Form1::OnAddCurrentCommandClick(System::Object ^ obj, System::EventArgs ^ e) {

	// Disabling the button
	button2->Enabled = false;

	// Parsing inputs
	String^ channel = Convert::ToString(numericUpDown1->Value);
	String^ note = Convert::ToString(numericUpDown2->Value);
	String^ volume = Convert::ToString(numericUpDown3->Value);
	String^ status = comboBox1->Text;

	// If no row before, add it
	if (dataGridView1->RowCount == 0) {
		dataGridView1->Rows->Add(textBox1->Text, channel, status, note, volume);
	}
	else {
		bool rowFound = false;
		System::Windows::Forms::DataGridViewRow^ row;
		System::Windows::Forms::DataGridViewCell^ cell;
		for (int r = 0; r < dataGridView1->RowCount; r++) {
			row = dataGridView1->Rows[r];
			for (int c = 0; c < row->Cells->Count; c++) {
				cell = row->Cells[c];
				if (cell->Value->Equals(textBox1->Text)) {
					row->SetValues(textBox1->Text, channel, status, note, volume);
					rowFound = true;
					break;
				}
			}
			if (rowFound) {
				break;
			}
		}
		if (!rowFound) {
			dataGridView1->Rows->Add(textBox1->Text, channel, status, note, volume);
		}
	}

	//Enabling the test button again
	button2->Enabled = true;

	// Enabling the save/play/delete button
	button4->Enabled = true;
	button6->Enabled = true;
	button9->Enabled = true;

}

void Form1::OnDeleteCurrentCommandClick (System::Object ^ obj, System::EventArgs ^ args) {
	// Disable the buttons
	button4->Enabled = false;
	button6->Enabled = false;
	button9->Enabled = false;

	for (int i = 0; i < dataGridView1->SelectedCells->Count; i++) {
		int rowindex = dataGridView1->SelectedCells[i]->RowIndex;
		dataGridView1->Rows->RemoveAt(rowindex);
	}
	// Enabling the button if there is still  some rows
	if (dataGridView1->RowCount != 0) {
	    button4->Enabled = true;
		button6->Enabled = true;
		button9->Enabled = true;
	}

}